﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Data;

namespace Kevin.Framework.Data.Sqlite
{
    public class SqliteDataBase:Database
    {
        protected char parameterToken = '@';

        public SqliteDataBase(string name):base(name)
        {
            this.factory = SQLiteFactory.Instance;
        }

        public override Provider.BuilderProvider Provider
        {
            get { return new Provider.SqliteProvider(); }
        }
        public override string BuildParameterName(string parameterName)
        {
            return (parameterName[0] != parameterToken) ? Convert.ToString(parameterToken) + parameterName : parameterName;
        }

        #region abstract methods

        protected override void DeriveParameters(System.Data.Common.DbCommand discoveryCommand)
        {
            //SQLiteCommandBuilder
        }

        protected override string DefaultReturnValueParameterName
        {
            get
            {
                return "@RETURN_VALUE";
            }
        }

        public override string GetDBStringLengthFunction(string data, bool addQuotation)
        {
            return string.Format("LEN({0})", AddQuotation(data, addQuotation));
        }

        public override string GetDBByteLengthFunction(string data, bool addQuotation)
        {
            return string.Format("DATALENGTH({0})", AddQuotation(data, addQuotation));
        }

        public override string GetDBSubStringStr(string data, int start, int length, bool addQuotation)
        {
            return string.Format("SUBSTRING({0}, {1}, {2})", AddQuotation(data, addQuotation), start, length);
        }

        public override string FormatDateTime(DateTime dt)
        {
            return this.AddQuotation(dt.ToString("yyyyMMdd HH:mm:ss.fff"), true);
        }

        public override string DBNullToString(string data, string nullStr, bool addQuotation)
        {
            return string.Format("ISNULL({0}, {1})", AddQuotation(data, addQuotation), nullStr);
        }

        public override string DBCurrentTimeFunction
        {
            get { return "GETDATE()"; }
        }

        public override string DBStrConcatSymbol
        {
            get { return "+"; }
        }

        public override string DBStatementBegin
        {
            get { return string.Empty; }
        }

        public override string DBStatementEnd
        {
            get { return string.Empty; }
        }

        public override string DBStatementSeperator
        {
            get { return "\n"; }
        }

        public override System.Data.IDbDataParameter CreateParameter(Builder.SqlClauseBuilderItemIUW data)
        {
            IDbDataParameter para = new SQLiteParameter();
            para.ParameterName = this.BuildParameterName(data.DataField);

            if (data.Data is DBNull)
            {
                para.Value = DBNull.Value;
                if (data.Data.GetType() == typeof(byte[]))
                {
                    para.DbType = DbType.Binary;
                }
            }
            else
            {
                para.Value = data.Data;
            }
            //para
            return para;
        }

        public override string GetPageSpliteSql(string sql, int pageSize, int pageIndex)
        {
            int from = Math.Max(pageIndex - 1 * pageSize, 0);
            int to = pageIndex * pageSize;
            string result = string.Format("{0} limit {1},{2}", sql, from, to);
            return result;
        }

        #endregion

    }
}
